const https = require('https');
const http = require('http');
var iconv = require('iconv-lite');
const fs = require('fs');
const path = require('path');

function str_error(error) {
    var msg = null;
    if (error) {
        if (error.message && error.stack) {
            msg = "messge: " + error.message + "\nstack: " + error.stack;
        } else if (error.message)
            console.log("messge:" + error.message);
    } else
        msg = "";
    return msg;
}

var Crawler = function () {

    return {
        requiredModules: [
            "https"
        ],

        createHttpsRequest: function () {
            const options = {
                hostname: 'm.canet.com.cn',
                port: 443,
                path: '/view-596034-1.html',
                method: 'GET'
            }

            const req = https.request(options, res => {
                console.log(`statusCode: ${res.statusCode}`)

                res.on('data', d => {
                    process.stdout.write(d)
                })
            })

            req.on('error', error => {
                console.error(str_error(error));
            })

            req.end()
        },

        createHttpRequest: function () {
            return new Promise(function (resolve, reject) {
                const options = {
                    hostname: 'www.canet.com.cn',
                    port: 80,
                    path: '/kemu/596034.html',
                    method: 'GET'
                }
                let rawData = [];
                let rawDataLen = 0;
                const req = http.request(options, res => {
                    res.on('data', d => {
                        rawData.push(d);
                        rawDataLen += d.length
                    });
                    res.on('end', () => {
                        try {
                            var rawBuffer = Buffer.concat(rawData, rawDataLen);
                            var decodedBuffer = iconv.decode(rawBuffer, 'gbk');
                            resolve(decodedBuffer);
                        } catch (e) {
                            reject(e);
                        }
                    });
                });
                req.on('error', reject);
                req.end();
            });
        }
    };
}

var oCrawler = Crawler();
oCrawler.createHttpRequest().then(function (data) {
    console.log("done");
    fs.writeFileSync(path.resolve("page.html"), data);
}).catch(function (error) {
    console.error(str_error(error));
});